import {
    login,
    logout,
    logOut
    // getInfo
} from '@/api/login'
import {
    getToken,
    setToken,
    removeToken,
    // setUserName,
} from '@/utils/auth'
import md5 from 'js-md5'
import {
    saveObjArr,
    getObjArr
} from "@/utils/ruoyi.js"
import WebIM from '../../utils/WebIM/WebIM'
import {
    dynamicRoute
} from "@/router/dynamicRoute.js"
const user = {
    state: {
        token: getToken(),
        username1: '',
        password: '',
        // id: '',
        // avatar: '',
        // roles: [],
        permissions: [],
        // deptId: ""
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        setUsername(state, username1) {
            state.username1 = username1;
        },
        setUserPwd(state, password) {
            state.password = password;
        },
        // SET_NAME: (state, name) => {
        //     state.name = name
        // },
        // SET_ID: (state, id) => {
        //     state.id = id
        // },
        // SET_AVATAR: (state, avatar) => {
        //     state.avatar = avatar
        // },
        // SET_ROLES: (state, roles) => {
        //     state.roles = roles
        // },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        },
        // SET_DEPTID: (state, deptId) => {
        //     state.deptId = deptId
        // }
    },

    actions: {
        // 登录
        Login({
            commit
        }, userInfo) {
            const username = userInfo.username.trim()
            const password = md5(userInfo.password)
            return new Promise((resolve, reject) => {
                login(username, password).then(res => {
                    console.log(res.data, 123455)
                    setToken(res.data.token)
                    commit('setUsername', res.data.huanxin.username);
                    commit('setUserPwd', res.data.huanxin.password);

                    // saveObjArr('router', res.data.menus)
                    console.log(res.data.menus, 4444)
                    saveObjArr('router', dynamicRoute)
                    saveObjArr('contactUserList', [])
                    saveObjArr('permissions', res.data.button)
                    commit('SET_PERMISSIONS', res.data.button)
                    commit('SET_TOKEN', res.data.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        GetInfo() {
            console.log("登录了")
        },
        // 获取用户信息
        // GetInfo({ commit, state }) {
        //     return new Promise((resolve, reject) => {
        //         getInfo(state.token).then(res => {
        //             const user = res.data.user
        //             const avatar = user.avatar == "" ? require("@/assets/image/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
        //             if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
        //                 commit('SET_ROLES', res.data.roles)
        //                 commit('SET_PERMISSIONS', res.data.permissions)
        //             } else {
        //                 commit('SET_ROLES', ['ROLE_DEFAULT'])
        //             }
        //             commit('SET_NAME', user.userName)
        //             commit('SET_AVATAR', avatar)
        //             commit('SET_ID', user.userId)
        //             commit('SET_DEPTID', user.deptId)
        //             console.log(user.deptId, "user.deptId")
        //             resolve(res.data)
        //         }).catch(error => {
        //             reject(error)
        //         })
        //     })
        // },

        // 退出系统
        LogOut({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                logOut(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    // commit('SET_ROLES', [])
                    // commit('SET_PERMISSIONS', [])
                    removeToken()
                    localStorage.removeItem('vuex');
                    localStorage.removeItem('router');
                    localStorage.removeItem('permissions');
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // // 前端 登出
        // FedLogOut({ commit }) {
        //     return new Promise(resolve => {
        //         commit('SET_TOKEN', '')
        //         removeToken()
        //         resolve()
        //     })
        // }
    }
}

export default user